//
// Created by shaowenhan on 2020/7/22.
//

#include "WriteMDVRPResult.h"
#if defined(MDVRP) or defined(MDOVRP)
void WriteMDVRPResult::writeMDVRPResult(Instance &instance, Result *result, const string &fileName)
{
    ofstream outfile;
    outfile.open(fileName, ios::out);

    outfile<<result->getBestCost()<<endl;
    int vehicleNum = 1;
    int preDepot = -1;
    for(auto route: result->routes)
    {
        int load = 0;
        double routeTime = 0;
        for(int i = 1; i < route.size(); i++)
        {
            load += instance.nodeDemands[route[i]];
#if defined(MDOVRP)
            routeTime += instance.distances[route[i-1]][route[i]];
#elif defined(MDVRP)
            routeTime += instance.distances[route[i-1]][route[i]]+instance.serviceTimes[route[i]];
#endif
        }
        routeTime += instance.distances[route.back()][route.front()];
        if(route.front() != preDepot)
        {
            preDepot = route.front();
            vehicleNum = 1;
        }
        else
            vehicleNum += 1;
        outfile<<preDepot+1<<' '<<vehicleNum<<' '<<routeTime<<' '<<load<<' '<<route.front()+instance.cusNum+1<<' ';
        for(int i = 1; i < route.size(); i++)
            outfile<<route[i]-instance.depotNum+1<<' ';
        outfile<<route.front()+instance.cusNum+1<<endl;
    }

    outfile.close();
}
#endif